<?php

namespace Modules\Groupon;

use Modules\Groupon\Entities\GrouponRecord;
use Modules\Groupon\Entities\Groupon;

class Cron
{
	public function handle($schedule)
	{
		$that = $this;
		$schedule->call(function() use ($that) {
			// $that->everyMinute();
			// 暂停
		})->everyMinute();
	}

	public function everyMinute()
	{
		// 关闭过期活动
		$records = Groupon::where([
			'status' => 1,
			['end_at', '<', now()]
		])->get();

		foreach ($records as $record) {
			$record->closeByTimeout();
		}

		// 开始活动
		$records = Groupon::where([
			'status' => 0,
			['start_at', '<', now()]
		])->get();

		foreach ($records as $record) {
			$record->update([
				'status' => 1
			]);
		}



		//自动关闭未成团拼团
		$records = GrouponRecord::status('working')->where('end_at', '<', now())->get();
		foreach ($records as $record) {
			$record->closeByTimeout();
		}	
	}
}